﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using IronPython.Hosting;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;

namespace GUMM
{
    public partial class MediaBrowser : DockContent
    {
        public delegate void MediaSort();

        private TreeNode MovieNode;
        private TreeNode TVMainNode;
        private TreeNode ComicMainNode;
        private TreeNode AudioClipNode;
        private TreeNode MusicNode;
        private TreeNode VideoClipNode;
        private TreeNode MusicVideoNode;
        private TreeNode BookNode;


        List<MediaInfo> _medialist;

        List<MediaInfo> MediaList
        {
            get
            {
                return _medialist;
            }
            set
            {
                _medialist = value;
                LoadMedia();
            }
        }
        List<Movie> MovieList
        {
            get
            {
                List<Movie> tmpList = new List<Movie>(MediaList.OfType<Movie>());
                tmpList.Sort();
                return tmpList;
            }
        }

        public MediaBrowser()
        {
            InitializeComponent();
            //InitializeIronPython();
            this.MediaList = new List<MediaInfo>();

        }

        public MediaBrowser(List<MediaInfo> MediaList)
            : this()
        {
            // TODO: Complete member initialization
            this.MediaList = MediaList;
        }

        private void InitializeIronPython()
        {
            throw new NotImplementedException();
        }

        private void LoadMedia()
        {
            string strSelectedMediaType = tscmbMediaSelect.Text;
            VerifyNodes();

            if (treeView1.Nodes.Contains(MovieNode))
                treeView1.Nodes.Remove(MovieNode);
            if (treeView1.Nodes.Contains(TVMainNode))
                treeView1.Nodes.Remove(TVMainNode);
            if (treeView1.Nodes.Contains(MusicVideoNode))
                treeView1.Nodes.Remove(MusicVideoNode);
            if (treeView1.Nodes.Contains(VideoClipNode))
                treeView1.Nodes.Remove(VideoClipNode);
            if (treeView1.Nodes.Contains(MusicNode))
                treeView1.Nodes.Remove(MusicNode);
            if (treeView1.Nodes.Contains(AudioClipNode))
                treeView1.Nodes.Remove(AudioClipNode);
            if (treeView1.Nodes.Contains(ComicMainNode))
                treeView1.Nodes.Remove(ComicMainNode);
            if (treeView1.Nodes.Contains(BookNode))
                treeView1.Nodes.Remove(BookNode);

            switch (strSelectedMediaType)
            {
                case "All Media":
                    if (!treeView1.Nodes.Contains(MovieNode))
                        treeView1.Nodes.Add(MovieNode);
                    if (!treeView1.Nodes.Contains(TVMainNode))
                        treeView1.Nodes.Add(TVMainNode);
                    if (!treeView1.Nodes.Contains(VideoClipNode))
                        treeView1.Nodes.Add(VideoClipNode);
                    if (!treeView1.Nodes.Contains(MusicNode))
                        treeView1.Nodes.Add(MusicNode);
                    if (!treeView1.Nodes.Contains(AudioClipNode))
                        treeView1.Nodes.Add(AudioClipNode);
                    if (!treeView1.Nodes.Contains(ComicMainNode))
                        treeView1.Nodes.Add(ComicMainNode);
                    if (!treeView1.Nodes.Contains(BookNode))
                        treeView1.Nodes.Add(BookNode);
                    break;
                case "Movies":
                    if (!treeView1.Nodes.Contains(MovieNode))
                        treeView1.Nodes.Add(MovieNode);
                    break;
                case "TV Shows":
                    if (!treeView1.Nodes.Contains(TVMainNode))
                        treeView1.Nodes.Add(TVMainNode);
                    break;
                case "Music Videos":
                    if (!treeView1.Nodes.Contains(MusicVideoNode))
                        treeView1.Nodes.Add(MusicVideoNode);
                    break;
                case "Video Clips":
                    if (!treeView1.Nodes.Contains(VideoClipNode))
                        treeView1.Nodes.Add(VideoClipNode);
                    break;
                case "Music":
                    if (!treeView1.Nodes.Contains(MusicNode))
                        treeView1.Nodes.Add(MusicNode);
                    break;
                case "Audio Clips":
                    if (!treeView1.Nodes.Contains(AudioClipNode))
                        treeView1.Nodes.Add(AudioClipNode);
                    break;
                case "Comics":
                    if (!treeView1.Nodes.Contains(ComicMainNode))
                        treeView1.Nodes.Add(ComicMainNode);
                    break;
                case "Books":
                    if (!treeView1.Nodes.Contains(BookNode))
                        treeView1.Nodes.Add(BookNode);
                    break;
                default:
                    break;
            }
        }

        private void VerifyNodes()
        {
            if (MovieNode == null)
                LoadMovies();
            if (TVMainNode == null)
                LoadTvShows();
            if (MusicVideoNode == null)
                LoadMusicVideos();
            if (VideoClipNode == null)
                LoadVideoClips();
            if (MusicNode == null)
                LoadMusic();
            if (AudioClipNode == null)
                LoadAudioClips();
            if (ComicMainNode == null)
                LoadComics();
            if (BookNode == null)
                LoadBooks();
        }



        private void tscmbMediaSelect_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadMedia();
        }

        private void LoadMovies()
        {
            if (MovieNode == null)
            {
                MovieNode = new TreeNode("Movies");
                MovieNode.SelectedImageIndex = 1;
                MovieNode.ImageIndex = 0;
                MovieNode.Tag = MovieList;

                treeView1.Nodes.Add(MovieNode);
                pnlMovieSearch.BringToFront();
            }

            MovieNode.Nodes.Clear();

            foreach (Movie item in MediaList.OfType<Movie>())
            {

            }
        }

        private void LoadTvShows()
        {
            //TODO: Load TV Shows
        }
        private void LoadMusicVideos()
        {
            //TODO: Load Music Videos
        }

        private void LoadVideoClips()
        {
            //TODO: Load Video Clips
        }

        private void LoadMusic()
        {
            // TODO: Load Music
        }

        private void LoadAudioClips()
        {
           //TODO: Load Audio Clips
        }

        private void LoadComics()
        {
            //TODO: Load Comics
        }

        private void LoadBooks()
        {
            //TODO: Load Books
        }
    }
}
